package top.zk123.chain.service.imp;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.zk123.chain.bean.Institution;
import top.zk123.chain.bean.ReturnInfo;
import top.zk123.chain.bean.UploadPath;
import top.zk123.chain.exception.FileEmptyException;
import top.zk123.chain.exception.FileUploadException;
import top.zk123.chain.exception.RegisterFailedException;
import top.zk123.chain.exception.WrongFileTypeException;
import top.zk123.chain.mapper.InstitutionMapper;
import top.zk123.chain.service.InstitutionService;
import top.zk123.chain.shiro.TokenUtils;
import top.zk123.chain.util.GlobalUtils;

import javax.annotation.Resource;
import java.util.List;

import static top.zk123.chain.util.GlobalUtils.AVATAR_PATH;

/**
 * 机构相关 核心方法实现
 *
 * @author Ke Zhang
 * @since 1.0
 */
@Service
public class InstitutionServiceImp implements InstitutionService {

    @Resource
    CertificateServiceImp certificateServiceImp;

    /**
     * Hash 加密的盐
     **/
    private final String SALT = "#4d1*dlmmddewd@34%";
    @Resource
    private TokenUtils tokenUtils;
    @Resource
    private InstitutionMapper institutionMapper;


    @Override
    public ReturnInfo register(String username, String pwd, String nickname, MultipartFile avatar, MultipartFile[] proof)
            throws FileUploadException, WrongFileTypeException, FileEmptyException, RegisterFailedException {
        Institution institution = institutionMapper.findByInstitutionId(username);
        if (institution != null) {
            throw new RegisterFailedException();
        } else {
            institution = new Institution();
        }
        String password = new SimpleHash("SHA-512", pwd,
                this.SALT).toString();
        institution.setPassword(password);
        institution.setInstitutionId(username);
        String avatarPath = String.valueOf(certificateServiceImp.uploadImages(new MultipartFile[]{avatar}, AVATAR_PATH).getData());
        institution.setAvatar(avatarPath.substring(1, avatarPath.length() - 1));
        institution.setNickname(nickname);
        institutionMapper.save(institution);
        return new ReturnInfo(200, "用户创建成功,请等待审核", username);
    }


    @Override
    public ReturnInfo login(Institution institution) {
        // 从 数据库查询用户信息
        Institution user = institutionMapper.findByInstitutionId(institution.getInstitutionId());
        if (user == null || user.getPassword() == null || user.getRole() == null
                || user.getPermission() == null) {
            return new ReturnInfo(1002, "用户信息不存在", null);
        }
        String password = new SimpleHash("SHA-512", institution.getPassword(),
                this.SALT).toString();
        if (!password.equals(user.getPassword())) {
            return new ReturnInfo(1003, "密码错误", null);
        }
        // 生成 Token
        return new ReturnInfo(200, "Token生成成功", tokenUtils.createToken(user));
    }

    @Override
    public ReturnInfo setNickname(String nickname) {
        Institution is = institutionMapper.findByInstitutionId(GlobalUtils.getIdByToken());
        is.setNickname(nickname);
        institutionMapper.save(is);
        return new ReturnInfo(200, "昵称更新成功", nickname);
    }

    @Override
    public ReturnInfo setAvatar(MultipartFile multipartFile) throws FileUploadException,
            WrongFileTypeException, FileEmptyException {
        List<UploadPath> paths = GlobalUtils.filesUpload(new MultipartFile[]{multipartFile},
                GlobalUtils.TOMCAT_PATH + AVATAR_PATH);
        Institution is = institutionMapper.findByInstitutionId(GlobalUtils.getIdByToken());
        String webPath = GlobalUtils.WEB_PATH + paths.get(0).getNewFileName();
        is.setAvatar(webPath);
        return new ReturnInfo(200, "头像更新成功", webPath);
    }
}
